package com.cade.agriculturemall.view.personalCenter.personalInfo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.model.UserInfoEntity;
import com.cade.agriculturemall.view.main.MainActivity;
import com.cade.agriculturemall.widget.TitleIntentLayout;
import com.cade.agriculturemall.widget.photo.PhotoHandlerCallback;
import com.cade.agriculturemall.widget.photo.PhotoPicker;

import java.util.List;

/**
 * Created by Lobby on 2017/11/29.
 */

public class PersonalInfoActivity extends BaseActivity<PersonalInfoContract.IPresenter> implements PersonalInfoContract.IView {
    private ImageView mImageAvatar;
    private RelativeLayout mrLAvatar;
    private TitleIntentLayout mTTLayoutNickName;
    private TitleIntentLayout mTTLayoutPhone;
    private TitleIntentLayout mTTLayoutSex;
    private UserInfoEntity mEntity = new UserInfoEntity();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_personal_info);
    }


    @Override
    public boolean isChange() {
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void getInfo(UserInfoEntity entity) {
        if (entity != null) {
            mEntity = entity;
//            mTTLayoutNickName.setText("昵称", "昵称还是空的，快取个有逼格的名字吧");
            mTTLayoutNickName.setText("昵称", mEntity.getNickname() != null ? mEntity.getNickname() : "昵称还是空的，快取个有逼格的名字吧");
            mTTLayoutPhone.setText("注册手机号", mEntity.getMobilePhoneNumber());
            mTTLayoutSex.setText("性别", mEntity.getSex() != null ? mEntity.getSex() : "未设置");
            Glide.with(this).load(mEntity.getLogo_url()).asBitmap().placeholder(R.mipmap.defaule_circle_grey_ava_36dp).into(mImageAvatar);
        }
    }

    @Override
    public void setupView() {
        setupToolbar(true);
        mImageAvatar = (ImageView) findViewById(R.id.personer_info_image_avatar);
        mrLAvatar = (RelativeLayout) findViewById(R.id.personal_info_rl_avatar);
        mTTLayoutNickName = (TitleIntentLayout) findViewById(R.id.personer_info_ttlayout_nick_name);
        mTTLayoutPhone = (TitleIntentLayout) findViewById(R.id.personal_info_rl_phone);
        mTTLayoutSex = (TitleIntentLayout) findViewById(R.id.personer_info_rl_sex);
        Button modifyBtn = (Button) findViewById(R.id.personal_info_btn_modify);

        modifyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPresenter.isHasChange(mEntity))
                    upLoadModify();
            }
        });
        mrLAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goSelectPic();
            }
        });
        mTTLayoutNickName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeNickName();
            }
        });
        mTTLayoutSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSexMenu();
            }
        });


    }

    //   点击按钮 提交申请
    private void upLoadModify() {
        if (mPresenter.isHasChange(mEntity)) {
            AlertDialog.Builder builderChange = new AlertDialog.Builder(this)
                    .setTitle("确认修改？").setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mPresenter.upLoadInfo(mEntity);
                        }
                    }).setNegativeButton("否", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            goBack();
                        }
                    });
            builderChange.show();
        }
    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {
        mPresenter.downLoadInfo();
    }

    /**
     * 更换头像，弹出选择的窗口,openSinglePickerWithCrop 只允许选择一张图片
     */
    private List<String> mSelectPhotoUrls;

    private void goSelectPic() {
        new PhotoPicker(this).openSinglePickerWithCrop(new PhotoHandlerCallback() {
            @Override
            public void onSuccess(List<String> photoList) {
                mSelectPhotoUrls = photoList;
                if (mSelectPhotoUrls.size() != 0) {
                    mEntity.setLogo_url(photoList.get(0));
                    Glide.with(getApplicationContext())
                            .load(photoList.get(0)).asBitmap().placeholder(R.mipmap.defaule_circle_grey_ava_36dp)
                            .into(mImageAvatar);
                }
            }
        }, 1, 1, 200, 200);
    }

    /**
     * 弹出性别选择框
     */
    public void showSexMenu() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("选择性别");

        final String[] sexy = {"男", "女"};
        builder.setItems(sexy, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mTTLayoutSex.setText("性别", sexy[which]);
                mEntity.setSex(sexy[which]);
            }
        });
        builder.show();
    }

    /**
     * 弹出昵称编辑框
     */
    public void changeNickName() {
        final EditText mEt = new EditText(this);
        mEt.setSingleLine(true);
        mEt.setText(mEntity.getNickname());
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("编辑我的昵称").setView(mEt)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mEntity.setNickname(mEt.getText().toString());
                        mTTLayoutNickName.setText("昵称", mEntity.getNickname());
                    }
                })
                .setNegativeButton("取消", null);
        builder.show();
    }

    @Override
    public PersonalInfoContract.IPresenter setupPresenter() {
        return new PersonalInfoPresenter(this);
    }


    //          选择后退，返回上个页面时，收集信息。判断是否做修改，
//          是？弹出提示框：后退
    @Override
    public void onBackPressed() {
        if (mPresenter.isHasChange(mEntity)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("未保存信息，是否确认离开？");
            builder.setNegativeButton("取消", null);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();

        } else {
            super.onBackPressed();
        }
    }

    public void goBack() {
        super.onBackPressed();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_cash_record, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.toolbar));
        switch (item.getItemId()) {
            case R.id.homemenu:
                Intent intent1 = new Intent(getApplication(), MainActivity.class);
                startActivity(intent1);
                break;
            case R.id.personalmenu:
                Intent intent2 = new Intent(getApplication(), PersonalInfoActivity.class);
                startActivity(intent2);
                break;
        }
        return true;
    }

}
